package com.cy.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cy.bean.UserList;
import com.cy.service.ChangeService;
import com.cy.service.ChangeServiceImpl;

/**
 * Servlet implementation class ChangeServlet
 */
@WebServlet("/change")
public class ChangeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 设置前端页面传递回来的字符编码
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		ChangeService changeService = new ChangeServiceImpl();
		String action = request.getParameter("action");
		int userflag = 0;
		int gameflag = 0;
		if (action != null && action.equals("reset")) {
			int id = Integer.parseInt(request.getParameter("id"));
			int flag = changeService.resetPwd(id);
			if (flag == 1) {
				userflag = 1;
			} else {
				userflag = 2;
			}
			response.sendRedirect(request.getContextPath() + "/userlist?flag=" + flag);
		} else if (action != null && action.equals("delete")) {
			int id = Integer.parseInt(request.getParameter("id"));
			int state = Integer.parseInt(request.getParameter("state"));
			if (state == 0) {
				state = 1;
			} else {
				state = 0;
			}
			int flag = changeService.delete(id, state);
			if (flag == 1) {
				userflag = 3;
			} else {
				userflag = 4;
			}
			response.sendRedirect(request.getContextPath() + "/userlist?flag=" + userflag);
		} else if (action != null && action.equals("change")) {
			int id = Integer.parseInt(request.getParameter("id"));
			int state = Integer.parseInt(request.getParameter("state"));
			if (state == 0) {
				state = 1;
			} else {
				state = 0;
			}
			int flag = changeService.change(id, state);
			if (flag == 1) {
				gameflag = 1;
			} else {
				gameflag = 2;
			}
			response.sendRedirect(request.getContextPath() + "/gamelist?flag=" + gameflag);
		} else if (action != null && action.equals("buy")) {
			int userid = Integer.parseInt(request.getParameter("userid"));
			int gameid = Integer.parseInt(request.getParameter("gameid"));
			int flag = changeService.buy(userid, gameid);
			if (flag == 1) {
				gameflag = 1;
			} else {
				gameflag = 2;
			}
			response.sendRedirect(request.getContextPath() + "/buygame?flag=" + gameflag);
		} else if (action != null && action.equals("changeusr")) {
			String username = request.getParameter("username");
			int userid = Integer.parseInt(request.getParameter("userid"));
			int flag = changeService.changeusr(username, userid);
			if (flag == 1) {
				response.sendRedirect(request.getContextPath() + "/logout");
			} else {
				gameflag = 1;
				response.sendRedirect(request.getContextPath() + "/aboutme?flag=" + gameflag);
			}
		} else if (action != null && action.equals("changepwd")) {
			String password = ((UserList) request.getSession().getAttribute("gengraluser")).getPassword();
			String oldpwd = request.getParameter("oldpwd");
			String newpwd = request.getParameter("newpwd");
			int userid = Integer.parseInt(request.getParameter("userid"));
			if (password.equals(oldpwd)) {
				int flag = changeService.changepwd(newpwd, userid);
				if (flag == 1) {
					response.sendRedirect(request.getContextPath() + "/logout");
				}
			} else {
				gameflag = 2;
				response.sendRedirect(request.getContextPath() + "/aboutme?flag=" + gameflag);
			}
		}
	}
}